import { http } from '@/utils'

// 获取频道
export const getChannels = ()=>{
  return async dispatch=>{
    // 发起ajax
    const { channels } =await http.get('/channels')
    // 设置给redux
    dispatch({ type:'artciles/setChannel',payload:channels})
  }
}
// 获取文章列表数据
export const getArticle = (params={})=>{
  return async dispatch => {
     // 发起ajax
     const res =await http.get('/mp/articles',{ params })
     // 设置给redux
     dispatch({ type:'artciles/setArticles',payload:res})
  }
}
// 删除文章数据
export const delArticle = id =>{
  return async () =>{
    // 发起删除请求
    await http.delete('/mp/articles/'+id)
  }
}
// 添加文章数据
export const addArticle = (data,draft=false)=>{
  return async () =>{
    // 发起添加请求
    await http.post(`/mp/articles?draft=${draft}`,data)
  }
}
// 编辑文章数据
export const editArticle = (data,draft=false)=>{
  return async () =>{
    // 发起添加请求
    await http.put(`/mp/articles/${data.id}?draft=${draft}`,data)
  }
}
// 查询文章数据
export const getArticleInfo = id => {
  return async dispatch => {
    return await http.get('mp/articles/' + id)
  }
}